<?php
/**
 * Created by PhpStorm.
 * User: dx
 * Date: 16/2/18
 * Time: 下午14:56
 */
class MktProductController extends BaseController {

    /**
     * @Author:      dx
     * @DateTime:    2016-2-16 19:04:14
     * @Description: mkt 推广产品设置
     */

    public function promotion_package(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'city' => 'required',
                'place' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                break;
            }
            //验证token
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            //获取推广产品－推广产品包含的产品包
            $promotion_product = Product::where('place',Input::get('place'))
                ->where('city',Input::get('city'))
                ->where('type','promotion')
                ->where('status',1)
                ->where('start_date', '<', date('Y-m-d H:i:s', time()))
                ->where('stop_date', '>', date('Y-m-d H:i:s', time()))
                ->select('id','name','desc','package_id')
                ->orderBy('sort','asc')
                ->get();
            $arr_packages = array();
            foreach($promotion_product as $_pro){
                $package_id = array_unique(explode(",", rtrim($_pro->package_id, ',')));
                $opp = ProductPackage::whereIn('id',$package_id)
                    ->where('status',1)
                    ->select(array('id as product_package_id','name as product_package_name'))
                    ->get();
                $_pro->packages = $opp;
                $arr_packages[]=$_pro;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $arr_packages);
        } while (false);

        Log::info("info", array("system" => "mkt" ,"event" => "promotion_package", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-2-16 19:04:14
     * @Description: mkt 推广产品 单个推广包 包含服务
     */

    public function base_package_item(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'product_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                break;
            }
            //验证token
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            //获取推广包中包含的基础包 字符串 逗号分隔
            $product = Product::where('id',Input::get('product_id'))
                ->where('status',1)
                ->select('package_id','name')
                ->first();
            //根据基础包id 获取N 个基础包
            $package_id = array_unique(explode(",", rtrim($product->package_id, ',')));
            $package = ProductPackage::whereIn('id',$package_id)
                ->where('status',1)
                ->get();
            $final_res = array();
            foreach($package as $_p){
                $item_data = ProductPackItem::where('pack_id',$_p->id)
                    ->where('status',1)
                    ->orderBy('type', 'asc')
                    ->get();
                $items = array();
                foreach($item_data as $_item){
                    if($_item->type == 1){ //耗材
                        $item = SupplySeries::where('id',$_item->item_id)
                            ->where('status',1)
                            ->select(array('id as item_id','name as item_name','amount'))
                            ->get()->toArray();
                    }elseif($_item->type == 2){ //服务
                        $item = ServiceProductItem::where('id',$_item->item_id)
                            ->where('status',1)
                            ->select(array('id as item_id','name as item_name','time as amount'))
                            ->get()->toArray();
                    }
                    array_push($items,$item[0]);
                }
                $package_one = array(
                    'id' => $_p->id,
                    'package_name' => $_p->name,
                    'item' => $items
                );
                array_push($final_res,$package_one);
            }

            $result = array(
                'promotion_product_id' => Input::get('product_id'),
                'promotion_product_name' => $product->name,
                'base_packages' => $final_res
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);

        Log::info("info", array("system" => "mkt" ,"event" => "promotion_package", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


}